package Demo04.Demo03.Demo03;
 
 
 
import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;
 
public class Demo03_Map {
 
	public static void main(String[] args) {
		//1.创建集合
		TreeMap<Student,Integer> tm=new TreeMap<Student,Integer>();
		for(int i=20070301,j=10;i<=20070330;i++,j++)
		{
			int grade=(int) (40*Math.random()+60);
			 //2、往集合对象中添加元素
			 tm.put(new Student(grade,"同学"+j),i);
		}
		
		//3.遍历集合 ，排序完成	
		Set<Student> k=tm.keySet();
		Iterator<Student> it=k.iterator();
		
		while(it.hasNext()){
			Student key=it.next();
			Integer num=tm.get(key);
			
			System.out.println("学号："+num+"    "+"姓名："+key.name+"    "+"成绩:"+key.grade);
		}
	}
 
}
 
class Student implements Comparable<Student>{
	int grade;
	String name;
	
	public Student(int grade,String name){
		this.grade =grade;
		this.name=name;
	}
 
	@Override
	public int compareTo(Student o) {
		
		if(this.grade>o.grade)  
			return 1;  
			if(this.grade==o.grade)  
			{  //当成绩相同时，按照姓名排序
			 return this.name.compareTo(o.name);  
			}  
			return -1;  
 
	}
	
 
	
}